/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software Foundation,
 *    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

/*
 * ActionDialog.java
 *
 * Created on 2010-01-07, 22:18:44
 */

package JET.Frames.actionframe;

import JET.Actions.Action;
import JET.Actions.ActivateTorquesAction;
import JET.Actions.DeactivateTorquesAction;
import JET.Centers.ControlCenter;
import JET.Frames.DataModels.ActionTypeComboBoxModel;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Frames.actionframe.actionpanels.ActivateTorqueActionPanel;
import JET.Frames.actionframe.actionpanels.DeactivateTorqueActionPanel;
import JET.Frames.actionframe.actionpanels.RunJavaScriptCodeActionPanel;
import JET.SA.SA_Dialogs;
import JET.StaticActions;
import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 *
 * @author 057Ry
 */
public class ActionsDialog extends javax.swing.JDialog {

    static final ControlCenter CC = ControlCenter.getInstance();

    /** Creates new form ActionDialog */
    public ActionsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        actionTypeComboBox = new javax.swing.JComboBox();
        containerPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Actions");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Actions"));

        jList1.setModel(DataModelsFactory.getActionsListModel());
        jScrollPane1.setViewportView(jList1);

        jButton2.setText("Remove action from list");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2))
        );

        actionTypeComboBox.setModel(new ActionTypeComboBoxModel());
        actionTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                actionTypeComboBoxActionPerformed(evt);
            }
        });

        containerPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        containerPanel.setLayout(new java.awt.BorderLayout());

        jLabel1.setText("Choose action type");

        jButton3.setText("Close");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(containerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addComponent(actionTypeComboBox, 0, 358, Short.MAX_VALUE))
                        .addGap(12, 12, 12)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(actionTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(containerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /*public void updateUserInterface()
    {
        if (containerPanel.getComponentCount()>0)
        {
            if (containerPanel.getComponent(0) instanceof ActivateTorqueActionPanel)
            {
                ActivateTorqueActionPanel atap = (ActivateTorqueActionPanel)containerPanel.getComponent(0);
                atap.reloadActionListModel();
            }
        }
    }
     */
    
    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        SA_Dialogs.setVisibleActionsDialog(false);
        System.out.println("dupa");
    }//GEN-LAST:event_formWindowClosed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        SA_Dialogs.setVisibleActionsDialog(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void actionTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_actionTypeComboBoxActionPerformed

        String name;
        int index = actionTypeComboBox.getSelectedIndex();
        switch( index )
        {
            case 0:     // ActivateTorquesAction
                containerPanel.removeAll();
                name = ActivateTorquesAction.getActualID()>0 ?
                                "ActivateTorquesAction"+ActivateTorquesAction.getActualID() : "ActivateTorquesAction";

                containerPanel.add( new ActivateTorqueActionPanel(name), BorderLayout.CENTER);
                containerPanel.updateUI();
                break;
            case 1:
                containerPanel.removeAll();
                name = DeactivateTorquesAction.getActualID()>0 ?
                                "DeactivateTorquesAction"+DeactivateTorquesAction.getActualID() : "DeactivateTorquesAction";

                containerPanel.add( new DeactivateTorqueActionPanel(name), BorderLayout.CENTER);
                containerPanel.updateUI();
                break;
            case 2:
                containerPanel.removeAll();
                containerPanel.updateUI();
                break;
            case 3:
                containerPanel.removeAll();
                containerPanel.add( new RunJavaScriptCodeActionPanel(), BorderLayout.CENTER);
                containerPanel.updateUI();
                break;
        }
    }//GEN-LAST:event_actionTypeComboBoxActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int[] indices = jList1.getSelectedIndices();
        ArrayList<Action> aList = new ArrayList<Action>(indices.length);
        for(int i=0; i<indices.length; i++)
            aList.add( CC.AC.getAction(indices[i]) );

        for(Action a : aList)
            StaticActions.removeAction(a);
    }//GEN-LAST:event_jButton2ActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox actionTypeComboBox;
    private javax.swing.JPanel containerPanel;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
